<?php
$_doctorQuestionAndAnswers = array();
$row = 0;
$col = 0;
foreach ($doctorQuestionAndAnswers as $doctorQuestionAndAnswer)
{
    $_doctorQuestionAndAnswers[$row][$col] = $doctorQuestionAndAnswer;
    $col++;

    // make sure you create a row of 4 question-and-answers
    if (fmod($col, 3) == 0)
    {
        $col = 0;
        $row++;
    }
}
?>
<?php foreach ($_doctorQuestionAndAnswers as $_row): ?>
    <div class="doctor-question-and-answer-row">
        <?php foreach ($_row as $_doctorQuestionAndAnswer): ?>
            <div class="doctor-question">
                <h1 class="topic"><?php echo $_doctorQuestionAndAnswer->getTopic() ?></h1>
                <span class="question">
                    <?php echo $_doctorQuestionAndAnswer->getQuestion() ?>
                </span>
                <span class="question-icon"></span>
            </div>
            <div class="doctor-answer">
                <?php if ($_doctorQuestionAndAnswer->getAnswer() == ""): ?>
                    <button class="button invoke-answer-form" type="submit" doctorquestionandanswerid="<?php echo $_doctorQuestionAndAnswer->getId() ?>">Answer</button>
                <?php else: ?>
                    <p class="answer">
                        <?php echo $_doctorQuestionAndAnswer->getAnswer() ?>
                    </p>
                    <span class="answer-icon"></span>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>
<script type="text/javascript">
    //<![CDATA[
    $(document).ready(function(){
        $(".invoke-answer-form").colorbox({inline:true, width:"700px", height:"450px", href:"#answer-form", "close":closeHtml});
    });
    //]]>
</script>